<template>
    <div>
        <commpVxeTable
         :config="dataTableConfig" 
         :columns="columnsVxe" 
         :formItem="formItem"
         @checkboxChange="checkboxChange"
         ref="questionnaireManagementRef"
         >
            <template v-slot:externalOperations="{data}">
                <vxe-form-item>
                    <template>
                        <a-button class="blue-btn" type="primary" @click="addition()">新增</a-button>
                    </template>
                </vxe-form-item>
            </template>
            <template v-slot:operation_btn="{row}" data-desc="自定操作栏">
                <a-form layout="inline" class="search-form" >
                    <a-form-item >
                        <a-button class="red-btn" type="primary" @click="deleteClick(row.id)">删除</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="blue-btn" type="primary" @click="editClick(row)">编辑</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="blue-btn" type="primary" @click="exportClick(row.id,0,'问卷选项')">导出问卷选项</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="blue-btn" type="primary" @click="exportClick(row.id,1,'问卷条款')">导出问卷条款</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="blue-btn" type="primary" @click="statisticsClick(row)">问卷统计</a-button>
                    </a-form-item>
                    <a-form-item >
                        <a-button class="blue-btn" type="primary" @click="QRCodeClick(row)">问卷调查二维码</a-button>
                    </a-form-item>
                </a-form>
            </template>
        </commpVxeTable>
        <QRQueryResults ref="refQRQueryResults" :id="questionnaireId"/>
        <editTitle ref="editTitleRef" />
        <statistics ref="statisticsRef" />
    </div>
</template>
<script>
import {deleteBatchWjjbxx,exportWjWriteSelectxxData,exportWjWriteTkxxData} from './service/index'
import assessCheckApi from './service/api.js'
import commpVxeTable from '@/components/commp-vxe-table/commp-vxe-table.vue'
import QRQueryResults from './QRQueryResults.vue'
import editTitle from './editTitle.vue'
import statistics from './statistics.vue'
export default {
    components:{
        commpVxeTable,
        QRQueryResults,
        editTitle,
        statistics,
    },
    props:{
        fwjlx:{
            type:String,
            default: () => '',
            required:false
        },
    },
    data(){
        return{
            questionnaireId:'',
            formItem:[
                {
                    label: '问卷标题',
                    model: 'fwjbt',
                }
            ],
            dataTableConfig:{
                api: assessCheckApi.wjjbxxPageList,
                rowId: 'id',
                paramet:{
                    fwjlx:this.fwjlx
                }
            },
            columnsVxe:[
                { 
                    type: 'checkbox',
                    width: 60 ,
                    fixed: 'left'
                },{
                    title: '问卷标题', //列头显示文字
                    field: 'fwjbt',
                },{
                    title: '操作',
                    field: 'id',
                    slots: {
                        // 使用插槽模板渲染
                        default: 'operation_btn',
                    },
                    fixed: 'right',
                    width:595
            }],
            selectedRowKeysArr:[],
            visibleQRQrCode:false,
        }
    },
    watch:{

    },
    mounted(){
        
        
    },
    methods:{
        addition(){
            this.$refs.editTitleRef.visibleShow = true
            this.$refs.editTitleRef.getData({fwjlx:this.fwjlx})
        },
        checkboxChange(val){//获取当前选择的行
            //console.log('当前选择表格',val)
            this.selectedRowKeysArr = []
            val.forEach(item => {
                this.selectedRowKeysArr.push(item.id)
            });
        },
        deleteClick(val){
            var that = this
            this.$confirm({
                title:'您确认要删除吗？',
                // icon: createVNode(ExclamationCircleOutlined),
                onOk() {
                    deleteBatchWjjbxx({ids:val}).then(res=>{
                        if(res.code == 200){
                            that.$message.success(res.message)
                            that.$refs.questionnaireManagementRef.searchEvent()
                        }else{
                            that.$message.warning(res.message)
                        }
                    })
                },
                
            });
        },
        QRCodeClick(val){
            this.questionnaireId = val.id
            this.$refs.refQRQueryResults.visibleQRQrCode = true
            this.$refs.refQRQueryResults.getData(val)
            // this.questionnaireId = res.result.wjjbxx.id
            
        },
        statisticsClick(val){
            this.$refs.statisticsRef.getData(val)
            this.$refs.statisticsRef.visible = true
        },
        exportClick(id,num,name){
            var that = this
            
            if(!num){
                exportWjWriteSelectxxData({id:id}).then((data) => {
                    if (!data) {
                        that.$message.warning('文件下载失败')
                        return
                    }
                    if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), name + '.xls')
                    } else {
                        let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                        let link = document.createElement('a')
                        link.style.display = 'none'
                        link.href = url
                        link.setAttribute('download', name + '.xls')
                        document.body.appendChild(link)
                        link.click()
                        document.body.removeChild(link) //下载完成移除元素
                        window.URL.revokeObjectURL(url) //释放掉blob对象
                    }
                })
            }else{
                exportWjWriteTkxxData({id:id}).then((data) => {
                    if (!data) {
                        that.$message.warning('文件下载失败')
                        return
                    }
                    if (typeof window.navigator.msSaveBlob !== 'undefined') {
                        window.navigator.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), name + '.xls')
                    } else {
                        let url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
                        let link = document.createElement('a')
                        link.style.display = 'none'
                        link.href = url
                        link.setAttribute('download', name + '.xls')
                        document.body.appendChild(link)
                        link.click()
                        document.body.removeChild(link) //下载完成移除元素
                        window.URL.revokeObjectURL(url) //释放掉blob对象
                    }
                })
            }
            
        },
        editClick(val){
        //console.log(val)
            this.$refs.editTitleRef.visibleShow = true
            this.$refs.editTitleRef.getData(val)
        },

    }
}
</script>
<style>
.red-btn{
    border: 1px solid #ed5565 !important;
    background-color: #ed5565 !important;
}
</style>